package sharding_DB

import (
	"server/framework/orm/session/db"
	"server/framework/orm/session/dialect"
	"server/framework/orm/session/model"
	"server/framework/orm/session/value"
)

type ShardingDB struct {
	// key就是sharingSelector中dst的DB
	DBs map[string]*MasterSlavesDB
	db.Core
}

func (db *ShardingDB) GetCore() db.Core {
	return db.Core
}

type ShardingDBOption func(db *ShardingDB)

func WithShardingDBReflectValue() ShardingDBOption {
	return func(db *ShardingDB) {
		db.ValueCreator = value.NewReflectValue
	}
}

func WithShardingDBUnsafeValue() ShardingDBOption {
	return func(db *ShardingDB) {
		db.ValueCreator = value.NewUnsafeValue
	}
}

func WithShardingDBDialect(dialect2 dialect.Dialect) ShardingDBOption {
	return func(db *ShardingDB) {
		db.Dialect = dialect2
	}
}

func NewShardingDB(dbs map[string]*MasterSlavesDB, opts ...ShardingDBOption) *ShardingDB {
	db2 := &ShardingDB{
		Core: db.Core{
			R:            &model.Register{},
			Dialect:      dialect.NewMysqlDialect(),
			ValueCreator: value.NewReflectValue,
		},
		DBs: dbs,
	}

	for _, opt := range opts {
		opt(db2)
	}
	return db2
}
